/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: Wangyaoxi
 * Date: 2010-4-19 下午02:36:06
 */
package com.ccxe.manage.service.page.service;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ccxe.manage.dao.AbstractPageDao;
import com.ccxe.manage.service.impl.BaseRequestService;
import com.ccxe.manage.service.page.bean.PageBean;
import com.ccxe.manage.util.StringValueValid;

/**
 * 分页业务 基类
 * 
 * @author wyx
 * 
 */
public abstract class PageService extends BaseRequestService {

	// 分页bean
	protected PageBean pb;

	@Override
	public void buildParameters() {

		try {
			if (pb == null) {
				pb = new PageBean();
			}
			// 由前台传值更新分页类信息
			this.initPageInfo();
			// 进行其他操作
			this.processBusiness();
			// 取得分页数据
			request.setAttribute("list", initPageList());
			// 创建pagebean中servlet地址
			pb.setServletUrl(pb.getServletUrl() == null ? buildServletUrl() : pb.getServletUrl());
			// 分页bean传到前台
			request.setAttribute("pagi", pb);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	private List<Map<?, ?>> initPageList() throws Exception {
		// 判断request来自查询按钮还是超链接
		String actionSgin = request.getParameter("actionSign");
		Map<String, String> valueMap = null;
		if ("fromLink".equals(actionSgin)) {
			valueMap = (Map<String, String>) request.getSession().getAttribute("pageMap");
		}

		if (valueMap == null) {
			// 初始化参数接收器
			valueMap = initPageMapParameters();
			request.getSession().setAttribute("pageMap", valueMap);
		}

		AbstractPageDao dao = initPageDao();
		return dao.initPageList(pb, valueMap);
	}

	protected abstract AbstractPageDao initPageDao();

	@SuppressWarnings("unchecked")
	protected Map<String, String> initPageMapParameters() {
		Map<String, String> result = new HashMap<String, String>();
		Enumeration items = request.getParameterNames();
		String key = null;
		while (items.hasMoreElements()) {
			key = (String) items.nextElement();
			result.put(key, (String) request.getParameter(key));
		}
		return result;
	}

	/**
	 * 初始化分页信息
	 */
	protected void initPageInfo() {
		// 分页信息
		String nowPage = request.getParameter("nowPage");
		String showData = request.getParameter("showData");
		if (StringValueValid.isNotNullAndEmpty(nowPage)) {
			pb.setNowPage(Integer.parseInt(nowPage));
		}
		if (StringValueValid.isNotNullAndEmpty(showData)) {
			pb.setShowData(Integer.parseInt(showData));
			pb.setPageShowData(Integer.parseInt(showData));
		}
	}

	// pageBean 的servlet地址
	protected String buildServletUrl() {
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort();
		String pPath = request.getRequestURI();
		pPath = pPath.indexOf("?") == -1 ? pPath + "?aa=1" : pPath;
		return basePath + pPath;
	}

	// 与分页无关的其他业务或者接传值操作等
	protected abstract void processBusiness();

	public PageBean getPb() {
		return pb;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}
}
